﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Economo.UI.Views;
using Economo.UI.AccountsServiceReference;
using Economo.Services.BusinessEntities;

namespace Economo.UI.Presenters
{
    public class ManageCreditCardsPresenter : ManageEntitiesPresenter 
    {
        public ManageCreditCardsPresenter(IManageCreditCardsView view)
            : base(view)
        {

        }
        private IManageCreditCardsView CreditCardsView
        {
            get
            {
                return (IManageCreditCardsView)View;
            }
        }
        #region CRUD methods
        protected override void UpdateEntity(int rowIndex)
        {
            AccountsServiceClient client = new AccountsServiceClient();
            try
            {
                Economo.Services.BusinessEntities.CreditCard updatedCard= GetCardFromGridRow(rowIndex);

                client.UpdateCreditCard(new UpdateCreditCardRequest(updatedCard));
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
            finally
            {
                client.Close();
            }
        }
        protected override void DeleteEntity(object id)
        {
            AccountsServiceClient client = new AccountsServiceClient();
            try
            {
                CreditCardKey key = (CreditCardKey)id;
                client.DeleteCreditCardById(new DeleteCreditCardByIdRequest(key.BankId,key.CardId));
                List<CreditCard> cards = (List<CreditCard>)View.EntitiesList;
                CreditCard card = (from c in cards
                                       where (c.BankId == key.BankId && c.Id == key.CardId)
                                       select c).FirstOrDefault<CreditCard>();
                cards.Remove(card);
                View.EntitiesList = cards;
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
            finally
            {
                client.Close();
            }
        }
        protected override void AddNewEntity()
        {
            AccountsServiceClient client = new AccountsServiceClient();
            try
            {
                List<CreditCard> cards = (List<CreditCard>)View.EntitiesList;
                CreditCard newCard = new CreditCard();
                newCard.Description = "(replace)";
                if (CreditCardsView.SelectedBankId.HasValue)
                    newCard.BankId = CreditCardsView.SelectedBankId.Value;
                else
                    newCard.BankId = client.GetBanksByDescription(new GetBanksByDescriptionRequest("")).Banks[0].Id;
                newCard.Id = client.InsertCreditCard(new InsertCreditCardRequest(newCard)).CardId;
                cards.Add(newCard);

                View.EntitiesList = cards;
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
            finally
            {
                client.Close();
            }
        }

        #endregion
        #region Side Methods
        private Economo.Services.BusinessEntities.CreditCard GetCardFromGridRow(Int32 rowIndex)
        {
            Economo.Services.BusinessEntities.CreditCard cardOnGridRow = new Economo.Services.BusinessEntities.CreditCard();
            CreditCardKey key = (CreditCardKey)View.GetEntityIdFromGridRow(rowIndex);
            cardOnGridRow.Id = key.CardId;
            cardOnGridRow.BankId = key.BankId;
            cardOnGridRow.Description = View.GetStringColumnFromGridRow(rowIndex, "Description");
            cardOnGridRow.CardNumber = View.GetCellValueFromGridRow<Int64>(rowIndex, "CardNumber");
            cardOnGridRow.Expiration = View.GetCellValueFromGridRow<DateTime>(rowIndex, "Expiration");
            cardOnGridRow.SecurityCode = View.GetCellValueFromGridRow<Int16>(rowIndex, "SecurityCode");
            cardOnGridRow.Picture = CreditCardsView.GetCreditCardPictureFromGridRow(rowIndex);
                 
            return cardOnGridRow;
        }
        #endregion

        #region FillMethods

        public override void FillData()
        {
            AccountsServiceClient client = new AccountsServiceClient();            
            try
            {
                List<CreditCard> cards;                
                if (!CreditCardsView.SelectedBankId.HasValue)
                {
                    CreditCardsView.Banks = client.GetBanksByDescription(new GetBanksByDescriptionRequest("")).Banks;                    
                }
                cards = client.GetBankCreditCards(new GetCreditCardsRequest(CreditCardsView.SelectedBankId.Value)).Cards;
                View.EntitiesList = cards;

            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
            finally
            {
                client.Close();
            }
        }

        #endregion
        public struct CreditCardKey
        {
            public byte BankId
            { get; set; }
            public byte CardId
            { get; set; }
        }

    }
}
